﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Inventory_TransactionFormat_TransFormatEdit : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    string AccTypeId = "";
    string FinYear = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (Request.QueryString["tfoid"] != null && Request.QueryString["trantypeid"] != null && Request.QueryString["trantypename"] != null)
        {
            string tfoid = Request.QueryString["tfoid"].ToString();
            string trantypeid = Request.QueryString["trantypeid"].ToString();
            string trantypename = Request.QueryString["trantypename"].ToString();

            if (!IsPostBack)
            {
                Bind_TransType(trantypeid,trantypename);
                BindEditFields(tfoid, trantypeid, trantypename);
            }
        }
    }
    protected void Bind_TransType(string trantypeid,string trantypename)
    {
        ddlTransaction.Items.Add(new ListItem(trantypename,trantypeid));
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
       
        string msg = "";
        try
        {
            if (Validate())
            {
                string tfoid = Request.QueryString["tfoid"].ToString();
                UpdateTransactionFormatMain(tfoid);
                UpdateTranFormatTariff(tfoid);
                UpdateTranFormatRefCaption(tfoid);
                UpdateTranFormatNumbering(tfoid);
                UpdateACCTransactionMain(tfoid);
                Response.Redirect("TransFormatView.aspx?msg=u");
            }
        }
        catch (Exception ex)
        {
            msg = ex.Message;
            AlertBox(msg);
        }
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("TransFormatView.aspx");
    }
    protected void chkAmtIncTax_CheckedChanged(object sender, EventArgs e)
    {

        if (chkAmtIncTax.Checked == true)
        {
            chkPreTax.Checked = false;
            chkPostTax.Checked = false;
            chkPreTax.Enabled = false;
            chkPostTax.Enabled = false;

        }
        else if (chkAmtIncTax.Checked == false)
        {
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
        }
    }
    protected void chkCenvat_CheckedChanged(object sender, EventArgs e)
    {
        if (chkCenvat.Checked == true)
        {
            chkBillLevelTariff.Visible = true;
        }
        else if (chkCenvat.Checked == false)
        {
            chkBillLevelTariff.Checked = false;
            chkBillLevelTariff.Visible = false;
        }

    }
    protected void chkReference_CheckedChanged(object sender, EventArgs e)
    {
        if (chkReference.Checked == true)
        {
            panelReference.Enabled = true;
            txtRefHeading1.Enabled = true;
            txtRefHeading2.Enabled = true;
            txtRefHeading3.Enabled = true;
            txtRefHeading4.Enabled = true;
        }
        else if (chkReference.Checked == false)
        {
            panelReference.Enabled = false;
            txtRefHeading1.Enabled = false;
            txtRefHeading2.Enabled = false;
            txtRefHeading3.Enabled = false;
            txtRefHeading4.Enabled = false;
        }
    }
    protected void chkBillLevelTariff_CheckedChanged(object sender, EventArgs e)
    {
        if (chkBillLevelTariff.Checked == true)
        {
            panelTariffHeading.Enabled = true;
            txtTariffHeading.Enabled = true;
            txtTariffSubHeading.Enabled = true;
            txtTariffDescription.Enabled = true;
        }
        else if (chkBillLevelTariff.Checked == false)
        {
            panelTariffHeading.Enabled = false;
            txtTariffHeading.Enabled = false;
            txtTariffSubHeading.Enabled = false;
            txtTariffDescription.Enabled = false;
        }
    }
    protected void Reset_chekbox()
    {
        //Reset checkbox
        chkPrepareBy.Checked = false;
        chkCheckBy.Checked = false;
        chkAuthorized.Checked = false;
        chkRemark.Checked = false;
        chkNote.Checked = false;
        chkNarration.Checked = false;
        chkDelivery.Checked = false;
        chkReference.Checked = false;
        chkRateAmount.Checked = false;
        chkTrackPending.Checked = false;
        chkPrint.Checked = false;
        chkGenerateVat.Checked = false;
        chkTermConditions.Checked = false;
        chkPreTax.Checked = false;
        chkPostTax.Checked = false;
        chkDefualNarration.Checked = false;
        chkAmtIncTax.Checked = false;
        chkCenvat.Checked = false;
        chkBillLevelTariff.Checked = false;
        chkPostBillTCS.Checked = false;
        chkAddDescItem.Checked = false;
    }
    protected void chkShowItemName_CheckedChanged(object sender, EventArgs e)
    {
        if (chkShowItemName.Checked == true)
        {
            chkShowItemCode.Checked = false;
        }
        else if (chkShowItemName.Checked == false)
        {
            chkShowItemCode.Checked = true;
        }
    }
    protected void chkShowItemCode_CheckedChanged(object sender, EventArgs e)
    {
        if (chkShowItemCode.Checked == true)
        {
            chkShowItemName.Checked = false;
        }
        else if (chkShowItemCode.Checked == false)
        {
            chkShowItemName.Checked = true;
        }
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlType.SelectedValue == "0")
        {
            txtStartNo.Enabled = false;
            txtStartNo.Text = "0";
            txtWidthofNo.Enabled = false;
            chkResetYear.Enabled = false;
            chkFillZero.Enabled = false;
        }
        else if (ddlType.SelectedValue == "1")
        {
            txtStartNo.Enabled = true;
            txtWidthofNo.Enabled = true;
            txtStartNo.Text = "1";
            chkResetYear.Enabled = true;
            chkFillZero.Enabled = true;

        }
    }
    protected bool Validate()
    {
        strsql = "Select FORMATNAME from IVR_TRANFORMAT where FORMATNAME='" + txtFormatName.Text.Trim() + "' and TFOID!=" + Request.QueryString["tfoid"].ToString();
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Duplicate Format Name');", true);
            return false;
        }
        else
        {
            return true;
        }
    }
    //Bind the form fields for editing
    private void BindEditFields(string tfoid,string trantypeid,string trantypename)
    {

        EnableOptionsByTranTypeId(trantypeid);
        SqlParameter[] param = { 
                                  new SqlParameter("@TFOID",tfoid)
                               };
        DataTable DTMain = DU.getdatausingprocedure("USP_IVR_GETTRANFORMATDETAILBYTFOID", param);
        if(DTMain.Rows.Count>0)
        {
            txtFormatName.Text = DTMain.Rows[0]["FORMATNAME"].ToString();
            chkRateAmount.Checked =Convert.ToBoolean(DTMain.Rows[0]["DCWITHAMOUNT"].ToString());
            //DTMain.Rows[0]["ROUNDOFF_LID"].ToString();
            //DTMain.Rows[0]["DISCOUNT_LID"].ToString();
            if (DTMain.Rows[0]["TAXATITEM"].ToString().Equals("True"))
            {
                radioTItemLevel.Checked = true;
            }
            else
            {
                radioTBillLevel.Checked = true;
            }

            if (DTMain.Rows[0]["DISCATITEM"].ToString().Equals("0"))
            {
                radioDNone.Checked = true;
            }
            else if (DTMain.Rows[0]["DISCATITEM"].ToString().Equals("1"))
            {
                radioDBillLevel.Checked = true;
            }
            else
            {
                radioDItemLevel.Checked = true;
            }

            chkDefualt.Checked = Convert.ToBoolean(DTMain.Rows[0]["SETASDEFAULT"].ToString());
            chkAddDescItem.Checked = Convert.ToBoolean(DTMain.Rows[0]["ITEMADDNDESC"].ToString());
            chkAmtIncTax.Checked = Convert.ToBoolean(DTMain.Rows[0]["AMOUNTINCLUTAX"].ToString());

            chkReference.Checked = Convert.ToBoolean(DTMain.Rows[0]["TRANREF"].ToString());
            chkTermConditions.Checked = Convert.ToBoolean(DTMain.Rows[0]["TERMSCONDITIONS"].ToString());
            chkPreTax.Checked = Convert.ToBoolean(DTMain.Rows[0]["PRETAXCHARGES"].ToString());
            chkPostTax.Checked = Convert.ToBoolean(DTMain.Rows[0]["POSTTAXCHARGES"].ToString());
            chkNarration.Checked = Convert.ToBoolean(DTMain.Rows[0]["NARRATION"].ToString());

            chkDefualNarration.Checked = Convert.ToBoolean(DTMain.Rows[0]["DEFAULTNARRATION"].ToString());
            chkNote.Checked = Convert.ToBoolean(DTMain.Rows[0]["NOTE"].ToString());
            chkRemark.Checked = Convert.ToBoolean(DTMain.Rows[0]["REMARKS"].ToString());
            chkPrepareBy.Checked = Convert.ToBoolean(DTMain.Rows[0]["PREPAREDBY"].ToString());
            chkCheckBy.Checked = Convert.ToBoolean(DTMain.Rows[0]["CHECKEDBY"].ToString());
            chkAuthorized.Checked = Convert.ToBoolean(DTMain.Rows[0]["AUTHSIGNATORY"].ToString());
            chkPostBillTCS.Checked = Convert.ToBoolean(DTMain.Rows[0]["POSTBILLING"].ToString());
            chkPrint.Checked = Convert.ToBoolean(DTMain.Rows[0]["PRINTONSAVE"].ToString());

            if (DTMain.Rows[0]["ITEMTYPE"].ToString().Equals("0"))
            {
                chkShowItemName.Checked = true;
            }

            if (DTMain.Rows[0]["ITEMTYPE"].ToString().Equals("1"))
            {
                chkShowItemCode.Checked = true;
            }

            chkGenerateVat.Checked = Convert.ToBoolean(DTMain.Rows[0]["VAT505ONSAVE"].ToString());
            chkDelivery.Checked = Convert.ToBoolean(DTMain.Rows[0]["DELIVERYDETAILS"].ToString());
            ddlJrnlType.SelectedValue = DTMain.Rows[0]["STKJRNLOPT"].ToString();
            txtInvoiceHeading.Text = DTMain.Rows[0]["INVDCHEADING"].ToString();
            chkTrackPending.Checked = Convert.ToBoolean(DTMain.Rows[0]["DCTRACKPENDING"].ToString());
            txtDocumnetNo.Text = DTMain.Rows[0]["DOCNUMCAP"].ToString();
            txtDeclaration.Text = DTMain.Rows[0]["TRANDECLARATION"].ToString();
            chkCenvat.Checked = Convert.ToBoolean(DTMain.Rows[0]["CENVAT"].ToString());
            chkBillLevelTariff.Checked = Convert.ToBoolean(DTMain.Rows[0]["BILLLEVELTARIFF"].ToString());

            txtTariffHeading.Text = DTMain.Rows[0]["HEADING"].ToString();
            txtTariffSubHeading.Text = DTMain.Rows[0]["SUBHEADING"].ToString();
            txtTariffDescription.Text = DTMain.Rows[0]["TARIFFDESCRIPTION"].ToString();

            ddlType.SelectedValue = DTMain.Rows[0]["NUMBERINGMETHOD"].ToString();
            txtPrefix.Text = DTMain.Rows[0]["NUMPREFIX"].ToString();
            txtStartNo.Text = DTMain.Rows[0]["NUMSTART"].ToString();
            txtSuffix.Text = DTMain.Rows[0]["NUMSUFFIX"].ToString();
            txtWidthofNo.Text = DTMain.Rows[0]["NUMDIGITS"].ToString();
            chkFillZero.Checked = Convert.ToBoolean(DTMain.Rows[0]["NUMPREFILLZERO"].ToString());
            if (DTMain.Rows[0]["RESETOPT"].ToString().Equals("1"))
            {
                chkResetYear.Checked = true;
            }
           
            DataTable DTRefCap = DU.getdatausingprocedure("USP_IVR_GETTRANFORMATREFCAPBYTFOID", param);
            if (DTRefCap.Rows.Count > 0)
            {
                txtRefHeading1.Text = DTRefCap.Rows[0]["REFCAPTION"].ToString();
                txtRefHeading2.Text = DTRefCap.Rows[1]["REFCAPTION"].ToString();
                txtRefHeading3.Text = DTRefCap.Rows[2]["REFCAPTION"].ToString();
                txtRefHeading4.Text = DTRefCap.Rows[3]["REFCAPTION"].ToString();
            }


        }
        
    }
    //Enable and disable entry fields based on transaction type
    private void EnableOptionsByTranTypeId(string trantypeid)
    {
        if (trantypeid == "1")// Sales Invoice
        {
            lblHeading.Text = "Invoice Heading";

            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = true;
            radioDBillLevel.Enabled = true;
            radioDItemLevel.Enabled = true;
            radioDNone.Enabled = true;
            radioTBillLevel.Checked = true;
            radioTBillLevel.Enabled = true;
            radioTItemLevel.Enabled = true;
            txtRoundoff.Enabled = true;
            txtDiscount.Enabled = true;

            radioDNone.Checked = true;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;

            //************Tab Tax & Discount***********//

            chkAmtIncTax.Enabled = true;
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
            chkDelivery.Enabled = true;
            chkReference.Enabled = true;
            chkTermConditions.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkNarration.Enabled = true;
            chkDefualNarration.Enabled = true;
            chkRemark.Enabled = true;
            chkPrint.Enabled = true;
            chkGenerateVat.Enabled = true;
            chkCenvat.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkNote.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;

        }
        else if (trantypeid == "2")//Sales Return
        {
            lblHeading.Text = "DC Heading";
            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = true;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDNone.Checked = true;

            radioTBillLevel.Checked = true;
            radioTItemLevel.Enabled = false;
            txtRoundoff.Enabled = false;
            txtDiscount.Enabled = false;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;
            //************Tab Tax & Discount***********//
            chkDelivery.Enabled = true;
            chkReference.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkRemark.Enabled = true;
            chkNote.Enabled = true;
            chkRateAmount.Enabled = true;
            chkTrackPending.Enabled = true;
            chkPrint.Enabled = true;
            chkGenerateVat.Enabled = true;
            chkTermConditions.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkPreTax.Enabled = false;
            chkPostTax.Enabled = false;
            chkNarration.Enabled = false;
            chkDefualNarration.Enabled = false;
            chkAmtIncTax.Enabled = false;
            chkCenvat.Enabled = false;
        }
        else if (trantypeid == "3")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";
            //************Tab Tax & Discount***********//
            //panelDiscount.Enabled = false;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDNone.Checked = true;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioDNone.Enabled = false;

            radioTBillLevel.Checked = true;
            radioTBillLevel.Enabled = true;
            radioTItemLevel.Enabled = true;
            radioTItemLevel.Checked = false;
            txtRoundoff.Enabled = true;
            txtDiscount.Enabled = false;



            //************Tab Tax & Discount***********//
            chkAmtIncTax.Enabled = true;
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
            chkDelivery.Enabled = true;
            chkReference.Enabled = true;
            chkTermConditions.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkNarration.Enabled = true;
            chkDefualNarration.Enabled = true;
            chkRemark.Enabled = true;
            chkPrint.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkCenvat.Enabled = false;
            chkNote.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;
        }
        else if (trantypeid == "4")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";
            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = true;
            radioDBillLevel.Enabled = true;
            radioDItemLevel.Enabled = true;
            radioDNone.Checked = true;
            radioDNone.Enabled = true;

            radioTBillLevel.Checked = true;
            radioTBillLevel.Enabled = true;
            radioTItemLevel.Enabled = true;
            txtRoundoff.Enabled = true;
            txtDiscount.Enabled = true;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;

            //************Tab Tax & Discount***********//
            chkAmtIncTax.Enabled = true;
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
            chkDelivery.Enabled = true;
            chkReference.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkNarration.Enabled = true;
            chkDefualNarration.Enabled = true;
            chkRemark.Enabled = true;
            chkCenvat.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkPrint.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkTermConditions.Enabled = false;
            chkNote.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;
        }
        else if (trantypeid == "5")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";
            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = true;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDNone.Checked = true;

            radioTBillLevel.Checked = true;
            radioTItemLevel.Enabled = false;
            txtRoundoff.Enabled = false;
            txtDiscount.Enabled = false;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;
            //************Tab Tax & Discount***********//
            chkDelivery.Enabled = true;
            chkReference.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkRemark.Enabled = true;
            chkNote.Enabled = true;
            chkRateAmount.Enabled = true;
            chkTrackPending.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkPreTax.Enabled = false;
            chkPostTax.Enabled = false;
            chkNarration.Enabled = false;
            chkDefualNarration.Enabled = false;
            chkTermConditions.Enabled = false;
            chkAmtIncTax.Enabled = false;
            chkPrint.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkCenvat.Enabled = false;
        }
        else if (trantypeid == "6")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";
            //************Tab Tax & Discount***********//
            // panelDiscount.Enabled = false;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioDNone.Checked = true;
            radioDNone.Enabled = false;

            radioTBillLevel.Checked = true;
            radioTBillLevel.Enabled = true;
            radioTItemLevel.Enabled = true;
            radioTItemLevel.Checked = false;
            txtRoundoff.Enabled = true;
            txtDiscount.Enabled = false;



            //************Tab Tax & Discount***********//
            chkAmtIncTax.Enabled = true;
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
            chkDelivery.Enabled = true;
            chkReference.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkNarration.Enabled = true;
            chkDefualNarration.Enabled = true;
            chkRemark.Enabled = true;
            chkTermConditions.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkPrint.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkCenvat.Enabled = false;
            chkNote.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;
        }
        else if (trantypeid == "7")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";
            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = true;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDNone.Checked = true;

            radioTBillLevel.Checked = true;
            radioTItemLevel.Enabled = false;
            txtRoundoff.Enabled = false;
            txtDiscount.Enabled = false;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;
            //************Tab Tax & Discount***********//
            panelJrnlType.Visible = true;
            reqJournlStock.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkRemark.Enabled = true;
            chkNote.Enabled = true;
            chkNarration.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            chkDelivery.Enabled = false;
            chkReference.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;
            chkPrint.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkTermConditions.Enabled = false;
            chkPreTax.Enabled = false;
            chkPostTax.Enabled = false;
            chkDefualNarration.Enabled = false;
            chkAmtIncTax.Enabled = false;
            chkCenvat.Enabled = false;
        }
        else if (trantypeid == "8")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";
            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = false;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDNone.Checked = true;
            radioDNone.Enabled = false;

            radioTBillLevel.Checked = true;
            radioTItemLevel.Enabled = true;
            txtRoundoff.Enabled = true;
            txtDiscount.Enabled = false;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;

            //************Tab Tax & Discount***********//
            chkAmtIncTax.Enabled = true;
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
            chkDelivery.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkNarration.Enabled = true;
            chkDefualNarration.Enabled = true;
            chkRemark.Enabled = true;
            chkTermConditions.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            chkReference.Enabled = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkPrint.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkCenvat.Enabled = false;
            chkNote.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;
        }
        else if (trantypeid == "9")//Sales Return
        {
            lblHeading.Text = "Invoice/DC Heading";

            //************Tab Tax & Discount***********//
            panelDiscount.Enabled = false;
            radioDBillLevel.Enabled = false;
            radioDItemLevel.Enabled = false;
            radioDNone.Checked = true;
            radioDNone.Enabled = false;

            radioTBillLevel.Checked = true;
            radioTItemLevel.Enabled = true;
            txtRoundoff.Enabled = true;
            txtDiscount.Enabled = false;
            radioDItemLevel.Checked = false;
            radioDBillLevel.Checked = false;
            radioTItemLevel.Checked = false;

            //************Tab Tax & Discount***********//
            chkAmtIncTax.Enabled = true;
            chkPreTax.Enabled = true;
            chkPostTax.Enabled = true;
            chkDelivery.Enabled = true;
            chkTermConditions.Enabled = true;
            chkPrepareBy.Enabled = true;
            chkCheckBy.Enabled = true;
            chkAuthorized.Enabled = true;
            chkNarration.Enabled = true;
            chkDefualNarration.Enabled = true;
            chkRemark.Enabled = true;
            chkPrint.Enabled = true;
            chkShowItemName.Checked = true;

            chkShowItemCode.Checked = false;
            chkReference.Enabled = false;
            panelJrnlType.Visible = false;
            reqJournlStock.Enabled = false;
            chkGenerateVat.Enabled = false;
            chkCenvat.Enabled = false;
            chkNote.Enabled = false;
            chkRateAmount.Enabled = false;
            chkTrackPending.Enabled = false;
        }

    }
    private void AlertBox(string Message)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + Message + "');", true);
    }
    //Update the main transaction table
    private void UpdateTransactionFormatMain(string tfoid)
    {
        int RoundOff_Led = 0;
        string TaxAtItem = "";
        int Discount = 0;
        int Discount_Led = 0;
        string ItemType = "0";
        if (radioTBillLevel.Checked == true)
        {
            TaxAtItem = "False";
        }
        else if (radioTItemLevel.Checked == true)
        {
            TaxAtItem = "True";
        }
        if (radioDNone.Checked == true)
        {
            Discount = 0;
        }
        else if (radioDBillLevel.Checked == true)
        {
            Discount = 1;
        }
        else if (radioDItemLevel.Checked == true)
        {
            Discount = 2;
        }
        if (txtRoundoff.Text != string.Empty)
        {
            RoundOff_Led = 1;
        }
        else
        {
            RoundOff_Led = 0;
        }
        if (txtDiscount.Text != null)
        {
            Discount_Led = 1;
        }
        else
        {
            Discount_Led = 0;
        }
        if (chkShowItemName.Checked == true)
        {
            ItemType = "0";
        }
        else if (chkShowItemCode.Checked == true)
        {
            ItemType = "1";
        }
        string spname = "USP_IVR_UpdateTransactionFormat";
        SqlParameter[] sparam =
                                {
                                    new SqlParameter ("@TFOID",tfoid),
                                    new SqlParameter ("@TRANTYPE",ddlTransaction.SelectedValue),
                                    new SqlParameter ("@FORMATNAME",txtFormatName.Text),
                                    new SqlParameter ("@DCWITHAMOUNT",chkRateAmount.Checked),
                                    new SqlParameter ("@ROUNDOFF_LID",RoundOff_Led.ToString()),
                                    new SqlParameter ("@DISCOUNT_LID",Discount_Led.ToString()),
                                    new SqlParameter ("@TAXATITEM",TaxAtItem.ToString()),
                                    new SqlParameter ("@DISCATITEM",Discount.ToString()),
                                    new SqlParameter ("@SETASDEFAULT",chkDefualt.Checked),
                                    new SqlParameter ("@ITEMADDNDESC",chkAddDescItem.Checked),
                                    new SqlParameter ("@AMOUNTINCLUTAX",chkAmtIncTax.Checked),
                                    new SqlParameter ("@TRANREF",chkReference.Checked),
                                    new SqlParameter ("@TERMSCONDITIONS",chkTermConditions.Checked),
                                    new SqlParameter ("@PRETAXCHARGES",chkPreTax.Checked),
                                    new SqlParameter ("@POSTTAXCHARGES",chkPostTax.Checked),
                                    new SqlParameter ("@NARRATION",chkNarration.Checked),
                                    new SqlParameter ("@DEFAULTNARRATION",chkDefualNarration.Checked),
                                    new SqlParameter ("@NOTE",chkNote.Checked),
                                    new SqlParameter ("@REMARKS",chkRemark.Checked),
                                    new SqlParameter ("@PREPAREDBY",chkPrepareBy.Checked),
                                    new SqlParameter ("@CHECKEDBY",chkCheckBy.Checked),
                                    new SqlParameter ("@AUTHSIGNATORY",chkAuthorized.Checked),
                                    new SqlParameter ("@POSTBILLING",chkPostBillTCS.Checked),
                                    new SqlParameter ("@PRINTONSAVE",chkPrint.Checked),
                                    new SqlParameter ("@ITEMTYPE",ItemType.ToString()),
                                    new SqlParameter ("@VAT505ONSAVE",chkGenerateVat.Checked),
                                    new SqlParameter ("@DELIVERYDETAILS",chkDelivery.Checked),
                                    new SqlParameter ("@STKJRNLOPT",ddlJrnlType.SelectedValue),
                                    new SqlParameter ("@INVDCHEADING",txtInvoiceHeading.Text),
                                    new SqlParameter ("@DCTRACKPENDING",chkTrackPending.Checked),
                                    new SqlParameter ("@DOCNUMCAP",txtDocumnetNo.Text),
                                    new SqlParameter ("@TRANDECLARATION",txtDeclaration.Text),
                                    new SqlParameter ("@CENVAT",chkCenvat.Checked),
                                    new SqlParameter ("@BILLLEVELTARIFF",chkBillLevelTariff.Checked),
                                    new SqlParameter ("@ename",Session["username"].ToString())
                                };
         DU.executeprocedure(spname, sparam);
     }
    //Update the transaction tariff table
    private void UpdateTranFormatTariff(string tfoid)
    {
        string spname = "USP_IVR_UpdateTransactionFormatTariffDetail";
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TFOID",tfoid),
                                    new SqlParameter("@HEADING",txtTariffHeading.Text),
                                    new SqlParameter("@SUBHEADING",txtTariffSubHeading.Text),
                                    new SqlParameter("@TARIFFDESCRIPTION",txtTariffDescription.Text)
                                };
        DU.executeprocedure(spname, sparam);
     }
    //Update the transaction reference caption
    protected void UpdateTranFormatRefCaption(string tfoid)
    {
            string spname = "USP_IVR_UpdateTransactionReferenceCaptions";
            SqlParameter[] sparam1 =
                                {
                                    new SqlParameter("@TFOID",tfoid.ToString()),
                                    new SqlParameter("@REFID","1"),
                                    new SqlParameter("@REFCAPTION ",txtRefHeading1.Text),
                                    
                                };
            DU.executeprocedure(spname, sparam1);
            SqlParameter[] sparam2 =
                                {
                                    new SqlParameter("@TFOID",tfoid.ToString()),
                                    new SqlParameter("@REFID","2"),
                                    new SqlParameter("@REFCAPTION ",txtRefHeading2.Text),
                                    
                                };
            DU.executeprocedure(spname, sparam2);
            SqlParameter[] sparam3 =
                                {
                                    new SqlParameter("@TFOID",tfoid.ToString()),
                                    new SqlParameter("@REFID","3"),
                                    new SqlParameter("@REFCAPTION ",txtRefHeading3.Text),
                                    
                                };
            DU.executeprocedure(spname, sparam3);
            SqlParameter[] sparam4 =
                                {
                                    new SqlParameter("@TFOID",tfoid.ToString()),
                                    new SqlParameter("@REFID","4"),
                                    new SqlParameter("@REFCAPTION ",txtRefHeading4.Text),
                                    
                                };
            DU.executeprocedure(spname, sparam4);
      }
    //Update the transaction format numbering
    private void UpdateTranFormatNumbering(string tfoid)
    {
        if (txtWidthofNo.Text.Trim() == "")
        {
            txtWidthofNo.Text = "0";
        }
        string spname = "USP_IVR_UpdateTrasactionNumberFormat";
        SqlParameter[] sparam =
                            {
                                new SqlParameter("@TFOID",tfoid),
                                new SqlParameter("@FINYEAR","2010"),
                                new SqlParameter("@NUMBERINGMETHOD",ddlType.SelectedValue),
                                new SqlParameter("@NUMPREFIX",txtPrefix.Text.Trim()),
                                new SqlParameter("@NUMSTART",txtStartNo.Text.Trim()),
                                new SqlParameter("@NUMSUFFIX",txtSuffix.Text.Trim()),
                                new SqlParameter("@NUMDIGITS",txtWidthofNo.Text.Trim()),
                                new SqlParameter("@NUMPREFILLZERO",chkFillZero.Checked),
                                new SqlParameter("@RESETOPT",chkResetYear.Checked)
                            };
        DU.executeprocedure(spname, sparam);
    }
    //Update the transaction settings in ACC_TRANSETTINGS
    private void UpdateACCTransactionMain(string tfoid)
    {
        try
        {
            int AccParentTypeId = 0;
            if (ddlTransaction.SelectedValue.Equals("4"))
            {
                AccParentTypeId = 7;
            }
            else if (ddlTransaction.SelectedValue.Equals("6"))
            {
                AccParentTypeId = 8;
            }
            else if (ddlTransaction.SelectedValue.Equals("1"))
            {
                AccParentTypeId = 10;
            }
            else if (ddlTransaction.SelectedValue.Equals("3"))
            {
                AccParentTypeId = 11;
            }
            else
            {
                AccParentTypeId = 0;
            }


            string[] Date1 = string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(System.DateTime.Now)).ToString().Split('/');
            string effectiveDate = Date1[0].ToString() + "/" + Date1[1].ToString() + "/" + Date1[2].ToString();
            
            string typeid = "";
            DataTable DTType = DU.getdata("select TYPEID from ACC_TRANSETTINGS where billformatid=" + tfoid);
            typeid = DTType.Rows[0][0].ToString();

            SqlParameter[] sparam ={
                                        new SqlParameter("@TYPEID",typeid),
                                        new SqlParameter("@PARENTTYPEID",AccParentTypeId.ToString()),
                                        new SqlParameter("@TRANSACTIONNAME",txtFormatName.Text.Trim()),
                                        new SqlParameter("@ISAUTONO",ddlType.SelectedValue),
                                        new SqlParameter("@RESETNO",chkResetYear.Checked),
                                        new SqlParameter("@ISREFERENCE","0"),                                                
                                        new SqlParameter("@ISDEFAULTNARRATION","0"),
                                        new SqlParameter("@EFFECTIVEDATE",effectiveDate.ToString()),
                                        new SqlParameter("@BILLFORMATID",tfoid),
                                        new SqlParameter("@MODULETYPE","2"),
                                        new SqlParameter("@CCALLOCATIONTYPE","0")
                                     };
            int result = DU.executeprocedure("USP_ACC_SaveTransactionSetting", sparam);
            if (result > 0)
            {
                UpdateACCTransactionDetails(typeid);
            }
        }
        catch (Exception ex)
        {
            string str = ex.Message;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + str + "');", true);
        }
    }
    //Update the transaction settings in ACC_TRANSETTINGDETAILS
    private void UpdateACCTransactionDetails(string typeid)
    {
        try
        {
           SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TYPEID",typeid),
                                        new SqlParameter("@STARTNO",txtStartNo.Text.Trim()),
                                        new SqlParameter("@FINYEAR","2010"),
                                        new SqlParameter("@PREFIX",txtPrefix.Text.Trim()),
                                        new SqlParameter("@SUFFIX",txtSuffix.Text.Trim()),
                                        new SqlParameter("@ACCLEVELNARRATION","0"),
                                        new SqlParameter("@PRINTONSAVE","0")

                                    };
                DU.executeprocedure("USP_ACC_SaveTransactionSettingDetail", sparam);
        }
        catch (Exception ex)
        {
            string str = ex.Message;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + str.ToString() + "');", true);
        }

    }
}
